﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using Monkey.Warrenty.EntityModel;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.WarrantyTickets
{
    public partial class WaitingWarranty : System.Web.UI.Page
    {
        #region Properties

        private static WarrantyProductBiz _warrantyProductBiz;
        private static WarrantyProductBiz WarrantyProductManager
        {
            get { return _warrantyProductBiz ?? (_warrantyProductBiz = new WarrantyProductBiz()); }
        }
        
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.AdminBaoHanh))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if(!IsPostBack)
            {
                // load employees
                var employees = WarrantyProductManager.GetEmployees();
                employees.Insert(0, new Employee { Id = 0, Name = string.Empty });
                ddlEmployees.DataSource = employees;
                ddlEmployees.DataValueField = "Id";
                ddlEmployees.DataTextField = "Name";
                ddlEmployees.DataBind();

                // suppliers
                //var suppliers = WarrantyProductManager.GetSuppliers();
                //suppliers.Insert(0, new Supplier { Id = 0, Name = string.Empty });
                //ddlSuppliers.DataTextField = "Name";
                //ddlSuppliers.DataValueField = "Id";
                //ddlSuppliers.DataSource = suppliers;
                //ddlSuppliers.DataBind();
            }
        }

        [WebMethod(EnableSession = true)]
        public static object TicketProductList(int jtStartIndex, int jtPageSize, string jtSorting, string name, string code, string ticketCode)
        {
            var ticketCount = WarrantyProductManager.GetWaitingWarrantyProductsCount(name, code, ticketCode);
            var products = WarrantyProductManager.GetWaitingWarrantyProducts(jtStartIndex, jtPageSize, jtSorting, name, code,ticketCode);
            foreach (var product in products)
            {
                // product.CreatedTimeString = string.Format("{0:HH:mm}", product.CreatedDate);
                switch (product.ReceivedFrom)
                {
                    case 1:
                        product.ReceivedFromName = Resources.GlobalStrings.Customers;
                        break;
                    case 2:
                        product.ReceivedFromName = Resources.GlobalStrings.Employees;
                        product.DealerEmployeeName = product.FromEmployeeName;
                        break;
                    case 3:
                        product.ReceivedFromName = Resources.GlobalStrings.Dealers;
                        product.DealerEmployeeName = product.FromDealerName;
                        break;
                }
            }
            return new { Result = "OK", Records = products, TotalRecordCount = ticketCount };
        }

        [WebMethod(EnableSession = true)]
        public static object SendToWarrantyDep(List<long> productIds, int employeeId)
        {
            WarrantyProductManager.SendToWarrantyDep(productIds, employeeId);
            return new { Result = "OK" };
        }

        [WebMethod(EnableSession = true)]
        public static object SendToTechnical(List<long> productIds, int employeeId)
        {
            WarrantyProductManager.SendToTechnical(productIds, employeeId);
            return new {Result = "OK"};
        }

        [WebMethod(EnableSession = true)]
        public static object SendToWarrantySupplier(List<long> productIds, int supplierId, string note)
        {
            WarrantyProductManager.SendToSupplier(productIds, supplierId, note);
            return new { Result = "OK" };
        }
    }
}